import javax.swing.* ;
import java.awt.* ;
import java.util.LinkedList ;
import java.awt.event.* ;
import javax.swing.event.* ;
import java.io.File ;
import java.io.IOException ;


public class FiringGraph implements ActionListener {
	private LinkedList<PointPlot> curves ;
	private Graph g ;
	private JFrame window ;
	private static final JColorChooser colorChooser = new JColorChooser ( ) ;
	private static Color lastColor ;
	private String title ;
	
	private void newWindow ( ) {
		JPanel content = new JPanel ( ) ;
		JPanel controls = new JPanel ( new FlowLayout ( ) ) ;
		JCheckBox box ;
		Menu fileMenu = new Menu ( "File" ) , editMenu = new Menu ( "Edit" ) ;
		Menu viewMenu = new Menu ( "View" ) ;
		MenuItem item ;
		MenuBar menus = new MenuBar ( ) ;
		boolean addExit = true ;
		
		window = new JFrame ( ) ;		
		g = new Graph ( ) ;
		g.SAVE_WIDTH = 2048 ;
		g.SAVE_HEIGHT = 2048 ;
		
		item = new MenuItem ( "New" , new MenuShortcut( KeyEvent.VK_N ) ) ;     
		item.addActionListener ( this ) ;
		fileMenu.add ( item ) ;
		
		item = new MenuItem ( "Save", new MenuShortcut( KeyEvent.VK_S ) ) ;     
		item.addActionListener ( g ) ;
		fileMenu.add ( item ) ;
	
		item = new MenuItem ( "Close" , new MenuShortcut( KeyEvent.VK_W ) ) ;     
		item.addActionListener ( this ) ;
		fileMenu.add ( item ) ;
		
		fileMenu.addSeparator ( ) ;

		item = new MenuItem ( "Print" , new MenuShortcut( KeyEvent.VK_P ) ) ;
		item.addActionListener ( g ) ;
		fileMenu.add ( item ) ;		

		item = new MenuItem ( "Change Color" ) ;     
		item.addActionListener ( this ) ;
		editMenu.add ( item ) ;

		item = new MenuItem ( "Window" ) ;     
		item.addActionListener ( g ) ;
		viewMenu.add ( item ) ;


		// don't add an exit option on a mac; quit is already provided
		try {
			addExit = ! System.getProperty ( "os.name" ) . equals ( "Mac OS X" ) ;
		} catch ( Exception ex ) {
		}
		
		if ( addExit ) {
			fileMenu.addSeparator ( ) ;

			item = new MenuItem ( "Exit" , new MenuShortcut( KeyEvent.VK_Q ) ) ;     
			item.addActionListener ( this ) ;
			fileMenu.add ( item ) ;
		}
		
		menus.add ( fileMenu ) ;
		menus.add ( editMenu ) ;
		menus.add ( viewMenu ) ;
		window.setMenuBar ( menus ) ;
		
		for ( PointPlot c : curves ) {
			g.add ( c ) ;
			box = new JCheckBox ( ( String ) c.id ) ;
			box.setSelected ( c.isVisible ) ;
			box.addActionListener ( this ) ;
			controls.add ( box ) ;
		}
		g.autoWindow ( ) ;
		g.autoLabel ( ) ;
		
		window.addWindowListener ( Graph.NO_EXIT_ON_CLOSE ) ;
		content.setLayout ( new BoxLayout ( content , BoxLayout.Y_AXIS ) ) ;
		content.add ( g ) ;
		content.add ( controls ) ;
		window.add ( content ) ;
		window.setTitle ( title ) ;
		window.setSize ( 640 , 480 ) ;
		window.setVisible ( true ) ;
	}
	
	public FiringGraph ( LinkedList<PointPlot> curves , String title ) {
		this.title = title ;
		this.curves = curves ;
		newWindow ( ) ;
	}
	
	private void makeAnotherWindow ( ) {
		LinkedList<PointPlot> curves = new LinkedList<PointPlot> ( ) ;
		for ( PointPlot c : this.curves ) {
			curves.add ( c.copy ( ) ) ;
		}
		new FiringGraph ( curves , title ) ;
	}
	
	private void changeColorOfVisible ( ) {
	    Color color = colorChooser.showDialog ( null , "Choose Color For Visible" , lastColor ) ;
		if ( color != null ) {
			lastColor = color ;
			for ( PointPlot c : curves ) {
				if ( c.isVisible ) {
					c.setColor ( color ) ;
				}
			}
			g.refresh ( ) ;
		}
	}
		
	public void actionPerformed ( ActionEvent e ) {
		String choice ;
		choice = e.getActionCommand ( ) ;
		if ( choice.equals ( "New" ) ) {
			makeAnotherWindow ( ) ;
		} else if ( choice.equals ( "Close" ) ) {
			window.dispose ( ) ;
		} else if ( choice.equals ( "Exit" ) ) {
			System.exit ( 0 ) ;
		} else if ( choice.equals ( "Change Color" ) ) {
			changeColorOfVisible ( ) ;
		} else {
			g.flipVisible ( choice ) ;
		}
	}
}
